﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

namespace XDesigner.ORM
{
    [Serializable()]
    public struct ORMValue
    {
        public ORMValue( object rawValue )
        {
            this._Type = ORMValueType.Null;
            this._Value = null;
            this.RawValue = rawValue;
        }

        private ORMValueType _Type ;

        [System.ComponentModel.DefaultValue( ORMValueType.Null )]
        public ORMValueType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        private string _Value;
        [System.ComponentModel.DefaultValue( null)]
        public string Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        [System.ComponentModel.Browsable( false )]
        [System.Xml.Serialization.XmlIgnore()]
        public object RawValue
        {
            get
            {
                switch (_Type)
                {
                    case ORMValueType.Binary :
                        return Convert.FromBase64String(_Value);
                    case ORMValueType.Boolean :
                        return Convert.ToBoolean(_Value);
                    case ORMValueType.Byte :
                        return Convert.ToByte(_Value);
                    case ORMValueType.DateTime :
                        return Convert.ToDateTime(this._Value);
                    case ORMValueType.DBNull :
                        return DBNull.Value;
                    case ORMValueType.Decimal :
                        return Convert.ToDecimal(this._Value);
                    case ORMValueType.Double :
                        return Convert.ToDouble(this._Value);
                    case ORMValueType.Int16 :
                        return Convert.ToInt16(this._Value);
                    case ORMValueType.Int32 :
                        return Convert.ToInt32(this._Value);
                    case ORMValueType.Int64 :
                        return Convert.ToInt64(this._Value);
                    case ORMValueType.Null :
                        return null;
                    case ORMValueType.SByte :
                        return Convert.ToSByte(this._Value);
                    case ORMValueType.Single :
                        return Convert.ToSingle(this._Value);
                    case ORMValueType.String :
                        return this._Value;
                    case ORMValueType.UInt16 :
                        return Convert.ToUInt16(this._Value);
                    case ORMValueType.UInt32 :
                        return Convert.ToUInt32(this._Value);
                    case ORMValueType.UInt64 :
                        return Convert.ToUInt64(this._Value);
                    case ORMValueType.Char :
                        return Convert.ToChar(this._Value);
                    default :
                        return null;
                }
            }
            set
            {
                if (value == null)
                {
                    _Type = ORMValueType.Null;
                    _Value = null;
                }
                else if (DBNull.Value.Equals(value))
                {
                    _Type = ORMValueType.DBNull;
                    _Value = null;
                }
                else if (value is  byte[])
                {
                    _Type = ORMValueType.Binary;
                    _Value = Convert.ToBase64String((byte[])value);
                }
                else if (value is byte)
                {
                    _Type = ORMValueType.Byte;
                    _Value = Convert.ToString(value);
                }
                else if (value is sbyte)
                {
                    _Type = ORMValueType.SByte;
                    _Value = Convert.ToString(value);
                }
                else if (value is char)
                {
                    _Type = ORMValueType.Char;
                    _Value = Convert.ToString(value);
                }
                else if (value is bool)
                {
                    _Type = ORMValueType.Boolean;
                    _Value = Convert.ToString(value);
                }
                else if (value is DateTime)
                {
                    _Type = ORMValueType.DateTime;
                    _Value = Convert.ToString(value);
                }
                else if (value is decimal)
                {
                    _Type = ORMValueType.Decimal;
                    _Value = Convert.ToString(value);
                }
                else if (value is double)
                {
                    _Type = ORMValueType.Double;
                    _Value = Convert.ToString(value);
                }
                else if (value is short)
                {
                    _Type = ORMValueType.Int16;
                    _Value = Convert.ToString(value);
                }
                else if (value is ushort)
                {
                    _Type = ORMValueType.UInt16;
                    _Value = Convert.ToString(value);
                }
                else if (value is int)
                {
                    _Type = ORMValueType.Int32;
                    _Value = Convert.ToString(value);
                }
                else if (value is uint)
                {
                    _Type = ORMValueType.UInt32;
                    _Value = Convert.ToString(value);
                }
                else if (value is long)
                {
                    _Type = ORMValueType.Int64;
                    _Value = Convert.ToString(value);
                }
                else if (value is ulong)
                {
                    _Type = ORMValueType.UInt64;
                    _Value = Convert.ToString(value);
                }
                else if (value is float)
                {
                    _Type = ORMValueType.Single;
                    _Value = Convert.ToString(value);
                }
                else if (value is string)
                {
                    _Type = ORMValueType.String;
                    _Value = (string)value;
                }
                else
                {
                    _Type = ORMValueType.Null;
                    _Value = null;
                }
            }
        }
    }

    public enum ORMValueType
    {
        Null,
        DBNull,
        Char ,
        Boolean,
        Byte ,
        DateTime,
        Decimal,
        Double,
        Int16,
        Int32,
        Int64,
        SByte,
        Single,
        UInt16,
        UInt32,
        UInt64,
        String,
        Binary
    }
}
